import { baseRequestClient } from '#/api/request'
import { requestClient } from '#/api/request';
export async function getTubesApi() {
  return baseRequestClient.get('http://localhost:5320/api/gis/xzj-tube')
}

// ----------------------------------

// 查询管线返回值
interface ListPipeResult {
  rows: any;
  total: number;
}
// 添加管线参数值
export interface AddPipeParams {
  pipeCode: string;
  pipeName: string;
  pipeType: number;
  startPoint: string;
  endPoint: string;
  length: number;
  depth: number;
  diameter: number;
  material: string;
  pressureRatin: number;
  remark: string;

  startPoint_longitude: string;
  startPoint_latitude: string;
  endPoint_longitude: string;
  endPoint_latitude: string;
  geometry: string;
}
// 修改管线参数
export interface EditPipeParams {
  pipeCode: string;
  pipeName: string;
  pipeType: number;
  startPoint: string;
  endPoint: string;
  length: number;
  depth: number;
  diameter: number;
  material: string;
  pressureRatin: number;
  remark: string;

  startPoint_longitude: string;
  startPoint_latitude: string;
  endPoint_longitude: string;
  endPoint_latitude: string;
  geometry: string;
}
//查询管线列表在地图上展示
export const getPipesByGis = async () => {
  const response = await requestClient.get('/manage/pipeline/list');
  const data = response.rows; // 提取 rows 字段

  const parseGeometry = (geometry: string) => {
    const coords = geometry.match(/[-+]?\d*\.?\d+/g);
    if (!coords) return [];
    const points = [];
    // 假设 coords 是一个字符串数组
    for (let i = 0; i < coords.length; i += 2) {
      const x = coords[i] ?? '-1'; // 如果 coords[i] 为 undefined，则使用默认值 '-1'
      const y = coords[i + 1] ?? '-1'; // 如果 coords[i + 1] 为 undefined，则使用默认值 '-1'
      points.push([parseFloat(x), parseFloat(y)]);
    }
    return [points]; // 返回三维数组
  };

  const result = data.map((row: any) => {
    return {
      ...row,
      pipeType: row.pipeType === 0 ? '油气' : '注水',
      geometry: parseGeometry(row.geometry)
    };
  });
  return result;
};

// 查询管线（油气/注水）列表
export const listPipe = async (params: {
  pageNum: number;
  pageSize: number;
  // 可以根据需要添加其他查询参数
}) => {
  const response = await requestClient.get<ListPipeResult>('/manage/pipeline/list', { params });
  const data = response.rows; // 提取 rows 字段
  const total = response.total;
  const result = data;

  const mappedResult = result.map((row: any) => {
    return {
      ...row,
      pipeType: row.pipeType === 0 ? '油气' : '注水'
    };
  });

  return {
    items: mappedResult,
    total: total,
  };
};

// 查询管线（油气/注水）详细
export async function getPipe(id: number) {
  return await requestClient.get(`/manage/pipeline/${id}`);
}


// 新增管线（油气/注水）
export async function addPipe(data: any) {
  return await requestClient.post('/manage/pipeline', data);
}

// 修改管线（油气/注水）
export async function updatePipe(data: any) {
  return await requestClient.put('/manage/pipeline', data);
}

// 删除管线（油气/注水）
export async function delPipe(id: number) {
  return await requestClient.delete(`/manage/pipeline/${id}`);
}
